package com.example.ucenter.controller;

import com.example.common.commonsutils.R;
import com.example.ucenter.entity.Member;
import com.example.ucenter.entity.vo.LoginVO;
import com.example.ucenter.entity.vo.RegisterVO;
import com.example.ucenter.service.MemberService;
import io.jsonwebtoken.Header;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:tjf
 * @Description:
 */
@Tag(name = "用户中心登录控制器")
@RestController
@RequestMapping("/api/ucenter")
public class MemberController {
    @Resource
    private MemberService memberService;
    @Operation(description = "用户登录-根据用户名和密码进行登录")
    @PostMapping("/login")
    public R login(@RequestBody Member member){
        String token = memberService.loginByPhoneAndPassword(member);
        return R.ok().data("token", token);
    }

    @Operation(description = "用户注册-根据手机号和验证码")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVO){
        memberService.registerByPhoneAndCode(registerVO);
        return R.ok();
    }
    @Operation(description = "根据Token，获取用户的信息")
    @GetMapping("/info")
    public R getUserInfoByToken(@RequestHeader(name = "token", required = false) String token){
        if (StringUtils.isBlank(token)){
            return R.error().message("token无效，请登录！");
        }
        LoginVO loginMember = memberService.getUserInfoByToken(token);
        return R.ok().data("loginMember", loginMember);
    }
    @Operation(description = "根据id，获取会员的信息")
    @GetMapping("/{id}")
    public R getMemberInfoById(@PathVariable("id") String id){
        Member member = memberService.getById(id);
        LoginVO memberInfo = new LoginVO();
        //todo_tjf (tjf,2024/3/4,Com_T) 会员找不到
        if (member != null){
            BeanUtils.copyProperties(member, memberInfo);
            return R.ok().data("memberInfo", memberInfo);
        }
        return R.error().message("找不到该会员的信息");
    }
    @Operation(description = "统计指定日期的会员注册数")
    @GetMapping("/statistics/{datStr}")
    public Integer countRegisterMember(@PathVariable("datStr") String dayStr){
        Integer count = memberService.statisticsRegisterMember(dayStr);
        return count;
    }
}
